package com.squid.base.config.filter;

import com.squid.base.utils.HttpRequestUtils;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import jakarta.annotation.Resource;
import jakarta.servlet.*;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
@Slf4j
public class BruialPointFilter implements Filter {
    @Resource
    SimpleMeterRegistry simpleMeterRegistry;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("BruialPointFilter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (servletRequest instanceof HttpServletRequest) {
            String name="http.server.requests";
            String tag = "uri";
            HttpServletRequest request = HttpRequestUtils.getHttpServletRequest(servletRequest);
            String uri = request.getRequestURI();
            filterChain.doFilter(request, servletResponse);
            simpleMeterRegistry.counter(name, tag, uri).increment();
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    @Override
    public void destroy() {
        log.info("AuthFilter destroy");
    }
}